﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Descriptions_DomainModel.Model;
using Eco.Handles;
using Eco.Services;

namespace DomainModelEditor
{
    public partial class MethodEditForm : Form
    {
        private IUndoService undo;
        private bool result = false;

        public MethodEditForm(EcoSpace ecoSpace, Method method)
        {
            if (ecoSpace == null)
                throw new NullReferenceException("EcoSpace cannot be null!");
            if (method == null)
                throw new NullReferenceException("Method cannot be null!");

            InitializeComponent();

            undo = ecoSpace.GetEcoService<IUndoService>();

            if(undo == null)
                throw new NullReferenceException("UndoService must be registered in EcoSpace for MethodEditForm to work!");

            MethodVH.EcoSpace = ecoSpace;

            MethodVH.SetElementValue<Method>(method);
        }

        private void MethodEditForm_Load(object sender, EventArgs e)
        {
            undo.StartTransaction();
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            result = true;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            result = false;
            Close();
        }

        private void MethodEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (result)
                undo.CommitTransaction();
            else
                undo.RollbackTransaction();
        }
    }
}
